package com.dqp.cslggroup.UI;

import android.app.Dialog;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.dqp.cslggroup.R;

import java.util.Objects;

public class UpdateDialog extends Dialog {

    private UpdateDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {

        private View mLayout;
        private TextView newVersion;
        private TextView oldVersion;
        private TextView info;
        private TextView market;
        private TextView kuan;
        private TextView date;
        private UpdateDialog mDialog;

        public Builder(Context context) {
            mDialog = new UpdateDialog(context, R.style.Theme_AppCompat_Dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mLayout = inflater.inflate(R.layout.update_view, null, false);
            Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
            mDialog.addContentView(mLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            newVersion = mLayout.findViewById(R.id.tv_new_version);
            oldVersion = mLayout.findViewById(R.id.tv_old_version);
            info = mLayout.findViewById(R.id.tv_info);
            info.setMovementMethod(ScrollingMovementMethod.getInstance());
            market = mLayout.findViewById(R.id.tv_market);
            date = mLayout.findViewById(R.id.tv_date);
            kuan = mLayout.findViewById(R.id.tv_kuan);
        }

        /**
         * 设置 new version
         *
         * @return
         */
        public Builder setNewVersion(@NonNull String version) {
            newVersion.setText(version);
            newVersion.setVisibility(View.VISIBLE);
            return this;
        }

        /**
         * 设置 old version
         *
         * @return
         */
        public Builder setOldVersion(@NonNull String version) {
            oldVersion.setText(version);
            oldVersion.setVisibility(View.VISIBLE);
            return this;
        }

        /**
         * 设置 date
         *
         * @return
         */
        public Builder setDate(@NonNull String dates) {
            date.setText(dates);
            date.setVisibility(View.VISIBLE);
            return this;
        }


        /**
         * 设置 info
         *
         * @return
         */
        public Builder setInfo(@NonNull String infos) {
            info.setText(infos);
            return this;
        }

        /**
         * 设置按钮文字和监听
         *
         * @return
         */
        public Builder goMarket(@NonNull View.OnClickListener listener) {
            market.setOnClickListener(v -> {
                if (listener != null)
                    listener.onClick(v);
                mDialog.dismiss();
            });
            return this;
        }

        /**
         * 设置按钮文字和监听
         *
         * @return
         */
        public Builder goKuan(@NonNull View.OnClickListener listener) {
            kuan.setOnClickListener(v -> {
                if (listener != null)
                    listener.onClick(v);
                mDialog.dismiss();
            });
            return this;
        }

        public UpdateDialog create() {
            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);                //用户可以点击后退键关闭 Dialog
            mDialog.setCanceledOnTouchOutside(false);   //用户不可以点击外部来关闭 Dialog
            return mDialog;
        }
    }

}
